package after_sale_order

import (
	"context"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/after_sale_order"
	after_sale_orderReq "github.com/flipped-aurora/gin-vue-admin/server/model/after_sale_order/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/order"
)

type AfterSaleOrderService struct{}

// CreateAfterSaleOrder 创建售后管理记录
// Author [yourname](https://github.com/yourname)
func (afterSaleOrdersService *AfterSaleOrderService) CreateAfterSaleOrder(ctx context.Context, afterSaleOrders *after_sale_order.AfterSaleOrder) (err error) {
	err = global.GVA_DB.Create(afterSaleOrders).Error
	return err
}

// DeleteAfterSaleOrder 删除售后管理记录
// Author [yourname](https://github.com/yourname)
func (afterSaleOrdersService *AfterSaleOrderService) DeleteAfterSaleOrder(ctx context.Context, afterSaleId string) (err error) {
	err = global.GVA_DB.Delete(&after_sale_order.AfterSaleOrder{}, "after_sale_id = ?", afterSaleId).Error
	return err
}

// DeleteAfterSaleOrderByIds 批量删除售后管理记录
// Author [yourname](https://github.com/yourname)
func (afterSaleOrdersService *AfterSaleOrderService) DeleteAfterSaleOrderByIds(ctx context.Context, afterSaleIds []string) (err error) {
	err = global.GVA_DB.Delete(&[]after_sale_order.AfterSaleOrder{}, "after_sale_id in ?", afterSaleIds).Error
	return err
}

// UpdateAfterSaleOrder 更新售后管理记录
// Author [yourname](https://github.com/yourname)
func (afterSaleOrdersService *AfterSaleOrderService) UpdateAfterSaleOrder(ctx context.Context, afterSaleOrders after_sale_order.AfterSaleOrder) (err error) {
	err = global.GVA_DB.Model(&order.Order{}).Where("order_id = ?", afterSaleOrders.OrderId).Update("status", "5").Error
	err = global.GVA_DB.Model(&after_sale_order.AfterSaleOrder{}).Where("after_sale_id = ?", afterSaleOrders.AfterSaleId).Updates(&afterSaleOrders).Error
	return err
}

// GetAfterSaleOrder 根据afterSaleId获取售后管理记录
// Author [yourname](https://github.com/yourname)
func (afterSaleOrdersService *AfterSaleOrderService) GetAfterSaleOrder(ctx context.Context, afterSaleId string) (afterSaleOrders after_sale_order.AfterSaleOrder, err error) {
	err = global.GVA_DB.Where("after_sale_id = ?", afterSaleId).First(&afterSaleOrders).Error
	return
}

// GetAfterSaleOrderInfoList 分页获取售后管理记录
// Author [yourname](https://github.com/yourname)
func (afterSaleOrdersService *AfterSaleOrderService) GetAfterSaleOrderInfoList(ctx context.Context, info after_sale_orderReq.AfterSaleOrderSearch) (list []after_sale_order.AfterSaleOrder, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&after_sale_order.AfterSaleOrder{})
	var afterSaleOrderss []after_sale_order.AfterSaleOrder
	// 如果有条件搜索 下方会自动创建搜索语句

	if info.UserId != nil {
		db = db.Where("user_id = ?", *info.UserId)
	}
	if info.AfterSaleStatus != nil && *info.AfterSaleStatus != "" {
		db = db.Where("after_sale_status = ?", *info.AfterSaleStatus)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&afterSaleOrderss).Error
	return afterSaleOrderss, total, err
}
func (afterSaleOrdersService *AfterSaleOrderService) GetAfterSaleOrderPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
